package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomExcepton;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceimpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    //注入菜品分类接口
    @Autowired
    private DishService dishService;
    //注入套餐分类接口
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //查询菜品分类ID
        LambdaQueryWrapper<Dish> dish = new LambdaQueryWrapper<>();
        dish.eq(Dish::getCategoryId, id);
        //判断是否有关联菜品
        int count = dishService.count(dish);
        if (count > 0) {
//如果有关联菜品，则进行业务异常
            throw new CustomExcepton("菜品有关联不能删除");
        }


        //查询套餐分类ID
        LambdaQueryWrapper<Setmeal> setmeal = new LambdaQueryWrapper<>();
        setmeal.eq(Setmeal::getCategoryId, id);
        //判断是否有关联套餐
        int count1 = setmealService.count(setmeal);
        if (count1 > 0) {
//如果有关联套餐，则进行业务异常
            throw new CustomExcepton("有关联套餐，不能进行删除");
        }
        //如果既没有关联菜品，也没有关联套餐，则就可以进行删除
        super.removeById(id);
    }
}
